const fs = require('fs');
const moment = require('moment');
const { type } = require('os');
const path =require('path');
class Logger {

    //调用构造函数的时候赋值
    constructor(name,path,setJson) {
        this.fileName = name + '_' + moment().format("YYYYMMDD") + '.log' || '';
        this.path = path || '';
        this.setting = setJson || '';     //两种方式，通过set设置或者直接构造函数初始化
    }

    init(fileName,filePath) {
        this.fileName = fileName + '_' + moment().format("YYYYMMDD") + '.log' || '';
        this.path = filePath || '';
    }
    
    //传文件名，获取Logger的设置
    set(fileName) {
        if(!fs.existsSync(fileName)) {
            throw(`${fileName} is not exist`);      //配置文件不存在
        } else {
            let set = fs.readFileSync(fileName);
            try {
                this.setting = JSON.parse(set.toString());
            } catch (error) {
                throw(`setup file error catched:[${error}]`);
            }
            
        }
    }

    //判断这个对象是否是Json 是Json就转字符串 不然写入日志全是[obejct object]
    jsonCheck(text) {
        if(typeof text == 'object') {
            try {
                let a = JSON.stringify(text);
                return a;
            } catch(error){
                return text
            }
        } 
        return text ;   //不是对象没必要
    }

    writeLog(text) {
        //顺便输出
        if(!fs.existsSync(this.path)) {
            fs.mkdirSync(this.path);
        }
        let fileName = path.join(this.path,this.fileName);
        console.log(text);
        try {
            
            fs.appendFile(fileName,text,null,function(error){
                if(error) {
                    throw(`in appendFile  error = ${error}`);
                }
            });            //同步增加效率
        } catch(error) {
            console.log("error = ",error);
        }
        
    }

    parseText(args,type) {
        let logs = null;
        //处理 拼接日志信息 
        logs = `[${type}] ` + moment().format("YYYY-MM-DD HH:mm:ss") + " : ";
        if (typeof args != 'object' || args.length == 0) {
            logs += 'lack of log texts';
        } else {
            for (let i = 0; i < args.length; i++) {
                if(i == 0 ){
                    logs += this.jsonCheck(args[i]);
                } else {
                    logs += ' ' + this.jsonCheck(args[i]);
                }
                
            }
        }
        logs += '\n';       //最后一条得换行
        return logs;
    }

    debug(...args) {
        if (!this.fileName) {
            throw ("lack of log filename,please use constructor or set(fileName)");
        }
        if (!this.setting) {
            //暂时不做处理
        }
        
        this.writeLog(this.parseText(args,'debug'));
    }

    error(...args) {
        if (!this.fileName) {
            throw ("lack of log filename,please use constructor or set(fileName)");
        }
        if (!this.setting) {
            //暂时不做处理
        }
        
        this.writeLog(this.parseText(args,'error'));
    }

    log(...args) {
        if (!this.fileName) {
            throw ("lack of log filename,please use constructor or set(fileName)");
        }
        if (!this.setting) {
            //暂时不做处理
        }
        
        this.writeLog(this.parseText(args,'log'));
    }



}

exports = module.exports = Logger;
